package com.zkingsoft.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.Tesst;
import com.zkingsoft.dao.TesstDao;
import com.zkingsoft.service.TesstService;
import com.zkingsoft.common.bean.SysUsers;
/**
 * @description service接口实现类(用户管理)
 * @author jyy
 * @date 2017-12-29 10:45
 */
@Service
public class  TesstServiceImpl implements  TesstService{

	
	@Autowired
	private TesstDao tesstDao;
	
	
	@Override
	public int add(Tesst tesst){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		tesst.setCreateBy(loginUser.getSuName());
		tesst.setUpdateBy(loginUser.getSuName());
		tesst.setId(UUIDUtil.getRandomID());
		return tesstDao.insert(tesst);
		
	}
	
	@Override
	public int batchAdd(List<Tesst>  tesstList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = tesstList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= tesstList.size() ? tesstList.size() : end;
			List<Tesst> insertList = tesstList.subList(begin, end);
			num += tesstDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(Tesst oldTesst
	,Tesst newTesst){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldTesst, newTesst)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldTesst, newTesst);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newTesst);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("id", oldTesst.getId());
			tesstDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(Tesst tesst){
	
		return tesstDao.updateByModel(tesst);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return tesstDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String id){
	
		return tesstDao.deleteById(id);
	
	}
	
	@Override
	public int removeByModel(Tesst tesst){
	
		return tesstDao.deleteByModel(tesst);
	
	}
	
	
	@Override
	public List<Tesst> findInPage(Tesst tesst,  PaginationVO pageVo){
	
		return tesstDao.selectInPage(tesst , pageVo);
	
	}
	
	@Override
	public List<Tesst> findByModel(Tesst tesst){
	
		return tesstDao.selectByModel(tesst);
	
	}
	
	@Override
	public int  findTotal(Tesst tesst){
	
		return tesstDao.selectTotalRecord(tesst);
	
	}
	
	@Override
	public Tesst  findById(String id){
	
		return tesstDao.selectById(id);
	
	}

   	
	
	
}